<template>
	<view class="container">
		<fa-navbar title="通讯录"></fa-navbar>
		<view class="top_tab">
			<view class="left" @click="tabNav(0)" :class="{ current: tabCurrentIndex === 0 }">
				市工商联通讯录
				<view class="active" v-if="tabCurrentIndex === 0"> </view>
			</view>
			<view class="right" @click="tabNav(1)" :class="{ current: tabCurrentIndex === 1 }">
				商协会通讯录
				<view class="active" v-if="tabCurrentIndex === 1"> </view>
			</view>
		</view>
		<view class="search">
			<image src="/static/images/search.jpg" mode="" v-if="tabCurrentIndex === 0"></image>
			<input type="text" placeholder="搜索姓名关键字" placeholder-style="color:#b5b5b5;" v-if="tabCurrentIndex === 0"
				confirm-type="search" @confirm="doSearch" />
		</view>
		<view class="search">
			<image src="/static/images/search.jpg" mode="" v-if="tabCurrentIndex === 1"></image>
			<input type="text" placeholder="搜索商协会名称关键字" placeholder-style="color:#b5b5b5;" v-if="tabCurrentIndex === 1"
				confirm-type="search" @confirm="rightSearch" />
		</view>
		<view class="communicationList" v-if="tabCurrentIndex == 1">
			<scroll-view scroll-y="true" :style="{ height: scrollHeight }" @scrolltolower="ScollBottom" lower-threshold="200">
				<view class="communicationItem" v-for="(item, index) in organizationList" :key="index" @click="goDetail(item.id)">
					<view class="headPortrait">
						<img :src="item.logo"
							alt=""  />
					</view>
					<view class="name"> {{item.name}} </view>
				</view>
				<view v-if="no_more" style="text-align: center;">亲, 没有更多了</view>
			</scroll-view>
		</view>
		<view class="list_box" v-if="tabCurrentIndex == 0">
			<!-- 菜单左边 -->
			<view class="left">
				<scroll-view scroll-y="true" :style="{ height: scrollHeight }" >
					<view class="item" v-for="(item, index) in positionList" :key="index"
						:class="{ fz: item.name.length > 6  ,active: index == leftIndex}" :data-index="index" :data-id="item.id" @tap="leftTap">
						{{ item.name }} ({{item.user_num}})
					</view>
				</scroll-view>
			</view>
			<view class="main">
				<scroll-view scroll-y="true" :style="{ height: scrollHeight }" 
					:scroll-into-view="scrollInto" scroll-with-animation="true" 
					@scrolltolower="onReachScollBottom" lower-threshold="200">
					<block>
						<view class="item" v-for="(item, index) in industryList" :key="index">
							<view class="communicationItem" @click="open(item.id)">
								<view class="headPortrait">
									<!-- <image :src="encodeURI(item.avatar)" mode="aspectFill" alt="" /> -->
									<image :src="encodeURI(item.avatar)" mode="" alt="" />
								</view>
								<view class="name">
									<view class="name">
										{{ item.nickname }}
									</view>
									<view class="company">
										{{ item.company }}
									</view>
								</view>
							</view>
						</view>
						<view v-if="no_more" style="text-align: center;">亲, 没有更多了</view>
					</block>
				</scroll-view>
			</view>
		</view>
		<template>
			<view class="popup">
				<u-popup :show="show" border-radius="20" @close="close"  v-model="show" mode="center">
					<view class="message">
						<view class="left">
							<image :src="roleDetails.avatar" mode="aspectFill"></image>
						</view>
						<view class="right">
							<view class="name"> {{roleDetails.nickname}} </view>
							<view class="company"> {{roleDetails.company}}  </view>
							<view class="mobile"> {{roleDetails.secret_mobile}} </view>
						</view>
					</view>
					<view class="dialPhone" v-if="roleDetails.secret_mobile != ''" @tap="call(roleDetails.mobile)"> 拨打电话 </view>
					
				</u-popup>
			</view>
		</template>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				scrollHeight: '600px',
				leftIndex: 0,
				isMainScroll: false,
				scrollInto: '',
				tipsTop: '0px',
				// tabCurrentIndex: 0,
				pageInfo: {},
				tabCurrentIndex: 0,
				//左侧菜单
				leftMenuList: [],
				//选择的状态
				active: 0,
				// 右侧数据
				rightContent: [],
				scrollTop: 0,
				show: false,
				// 工商联总数据
				industryList: [],
				industryLists: [],
				// 工商联职位数据
				positionList: [],
				// 商协会数据
				organizationList: [],
				organizationLists:[],
				page: 1, // 当前页码
				no_more: false, //没有更多数据
				leftId: '', //左侧id
				keyword1: '' ,//左搜索关键词
				keyword2: '' ,//右搜索关键词
				roleDetails:[] ,//角色详情
				image:''
				
			}
		},
		onLoad () {
		// if(this.tabCurrentIndex == 0) {
			this.getIndustryLists()
		// }
			this.getPositionList()
			
		// },
		
		},
		computed: {

		},
		mounted() {
		

		},
		methods: {
			
			async onReachScollBottom() {
				// console.log(this.page,this.industryLists.last_page,'123');
					if (this.page >= this.industryLists.last_page) {
						this.no_more = true
						return false;
					}
					this.getIndustryLists(true, ++this.page);
			},
			async ScollBottom() {
			// console.log(this.page,this.organizationLists.last_page,'1222')
				// 加载下一页列表
					if (this.page >= this.organizationLists.last_page) {
						this.no_more = true
						return false;
					}
					this.organization(true, ++this.page);
			},
			
			async getIndustryLists(isPage, page) {
			// console.log();
				uni.showLoading({
					title: '加载中'
				});
				let data = {
					page: page || 1,
					group_id: this.leftId || 0,
					keyword: this.keyword1
				}
				let res = await this.$api.getIndustryList(data)
				console.log(res, 'res')
				if (!res.code) {
					this.$u.toast(res.msg)
					return
				}
				this.industryLists = res.data
				this.industryList = this.industryList.concat(res.data.data)
			},
			async getPositionList() {
				let res = await this.$api.getIndustryCount()
				console.log(res, 'res1')
				if (!res.code) {
					this.$u.toast(res.msg)
					return
				}
				this.positionList = res.data
			},

			async organization(isPage, page) {
				uni.showLoading({
					title: '加载中'
				});
				let data = {
					page: page || 1,
					keyword:this.keyword2
				}
				let res = await this.$api.getOrganization(data)
				if (!res.code) {
					this.$u.toast(res.msg)
					return
				}
				this.organizationLists = res.data
				// console.log(this.organizationLists,'this.organizationLists');
				this.organizationList = this.organizationList.concat(res.data.data)
			},

			//获取距离顶部的高度
			getScrollTop(selector) {
				return new Promise((resolve, reject) => {
					let query = uni.createSelectorQuery().in(this)
					query.select(selector + '').boundingClientRect(data => {
						resolve(data.top)
					}).exec()
				})
			},
			/* 获取元素顶部信息 */
			async getElementTop() {
				/* Promise 对象数组 */
				let p_arr = []

				/* 遍历数据，创建相应的 Promise 数组数据 */
				for (let i = 0; i < this.mainArray.length; i++) {
					const resu = await this.getScrollTop(`#item-${i}`)
					p_arr.push(resu)
				}

				// console.log('p_arr', p_arr)

				/* 主区域滚动容器的顶部距离 */
				this.getScrollTop("#scroll-el").then((res) => {

					let top = res
					// #ifdef H5
					top += 43 //因固定提示块的需求，H5的默认标题栏是44px
					// #endif

					/* 所有节点信息返回后调用该方法 */
					Promise.all(p_arr).then((data) => {
						console.log('滚动', data)
						this.tipsTop = `${data}px`
						this.topArr = data
					})
				})
			},

			
			/* 左侧导航点击 */
			leftTap(e) {
				console.log(e, 'eee');
				let index = e.currentTarget.dataset.index
				this.isMainScroll = false
				this.leftIndex = Number(index)
				this.scrollInto = `item-${index}`
				this.leftId = e.currentTarget.dataset.id
				this.industryList = []
				this.page = 1
				this.no_more = false
				this.getIndustryLists()

			},
			// 搜索框关键字
			doSearch(e) {
				console.log(e, 'ee')
				this.keyword1 = e.detail.value
				this.industryList = []
				this.getIndustryLists()
			},
			rightSearch(e) {
				console.log(e,'ee');
				this.keyword2 = e.detail.value
				this.organizationList = []
				this.organization()
			},
			// 拨号
			call(e) {
				
				let phone = e
				uni.makePhoneCall({
					phoneNumber: phone,
					success: function() {
						console.log('拨打电话成功')
					},
					fail() {
						console.log('打电话失败了')
					}
				})
			},
			// 获取用户详情
			

			// 打开弹窗
			async open(e) {
				// console.log(e,'id');
				this.show = true
				this.industryList.map(v => {
					// console.log(v,'vvv');
					if(e == v.id) {
					console.log(v,'vvv');
					this.roleDetails = v
					}
				})
				// this.organizationList = res.data
				
			},
			// 关闭弹窗
			close() {
				this.show = false
			},

			goDetail(id) {
				uni.navigateTo({
					url: `/pages/my/mailDetails?id=${id}`
				})
			},
			tabNav(index) {
				// console.log(index, 'index')
				this.tabCurrentIndex = index
				
				if(this.tabCurrentIndex == 1) {
					this.page = 1
					this.no_more = false
					this.organizationList = []
					this.keyword2 = ''
					this.organization()
				}else {
					this.page = 1
					this.no_more = false
					this.industryList = []
					this.keyword1 = ''
					this.getIndustryLists()
					
				}
			},
			//左侧菜单点击事件
			leftClickHandle(index) {
				this.active = index
				let rightContent = this.cates[index].children
				this.rightContent = rightContent
				this.scrollTop = 0
			},
			scroll(e) {
				this.scrollTop = e.detail.scrollTop
			},
			//跳转对应的id
			handleDetalis(url) {
				uni.navigateTo({
					url
				})
			},
		}
	}
</script>

<style lang="scss">
	/deep/ ::-webkit-scrollbar {
		display: block;
		width: 0px !important;
		height: 0px !important;
	}

	.container {
		height: 100%;

		.popup {
			::v-deep .u-drawer__scroll-view {
				padding: 30rpx 25rpx 30rpx 28rpx;
				width: 620rpx !important;
				// height: 483rpx !important;
				box-sizing: border-box !important;
				// border-radius: 10rpx;

				.message {
					display: flex;
					width: 100%;

					// background-color: green;
					.left {
						width: 212rpx;
						height: 282rpx;

						image {
							width: 100%;
							height: 100%;
						}
					}

					.right {
						flex: 1;
						padding-left: 34rpx;

						// background-color: red;
						// margin-top: -5rpx;
						.name {
							font-size: 30rpx;
							font-weight: bold;
						}

						.company {
							font-size: 26rpx;
							color: #656565;
							margin-top: 17rpx;
						}

						.mobile {
							margin-top: 7rpx;
							font-size: 26rpx;
							color: #656565;
						}
					}
				}

				.dialPhone {
					margin-top: 50rpx;
					width: 567rpx;
					height: 80rpx;
					text-align: center;
					background-color: #c72a29;
					color: #fff;
					line-height: 80rpx;
					font-size: 30rpx;
					border-radius: 10rpx;
				}
			}
		}

		.top_tab {
			display: flex;
			font-size: 34rpx;
			text-align: center;
			height: 97rpx;
			// line-height: 97rpx;
			padding-top: 28rpx;
			color: #a4a3a3;

			.left {
				width: 375rpx;

				.active {
					width: 69rpx;
					height: 6rpx;
					background-color: #d9001b;
					margin: 10rpx auto;
				}
			}

			.right {
				width: 375rpx;

				.active {
					width: 69rpx;
					height: 6rpx;
					background-color: #d9001b;
					margin: 10rpx auto;
				}
			}

			.current {
				color: #040000;
			}
		}

		.search {
			position: relative;

			image {
				width: 35rpx;
				height: 35rpx;
				position: absolute;
				left: 64rpx;
				top: 23rpx;
			}

			input {
				width: 670rpx;
				margin: 15rpx auto;
				height: 80rpx;
				padding-left: 77rpx;
				box-sizing: border-box;
				box-shadow: 0rpx 6rpx 10rpx 0rpx rgba(0, 0, 0, 0.04);
				border: solid 1rpx #e6e6e6;
			}
		}

		.communicationList {
			margin-top: 20rpx;

			.communicationItem {
				width: 670rpx;
				height: 130rpx;
				margin: auto;
				box-sizing: border-box;
				padding-top: 22rpx; // line-height: 130rpx;
				// padding-left: 117rpx;
				border-bottom: 1rpx solid #eee;

				.headPortrait {
					padding-left: 9rpx;
					
					image {
						width: 88rpx;
						height: 90rpx;
						border-radius: 50%;
					}
				}

				.name {
					padding-left: 117rpx;
					margin-top: -74rpx;
				}
			}
		}

		.list_box {
			display: flex;
			flex-direction: row;
			flex-wrap: nowrap;
			justify-content: flex-start;
			align-items: flex-start;
			align-content: flex-start;
			font-size: 28rpx;

			.left {
				width: 225rpx;
				background-color: #f6f6f6;
				line-height: 80rpx;
				box-sizing: border-box;
				font-size: 32rpx;

				.item {
					height: 130rpx;
					line-height: 130rpx;
					color: #333;
					background-color: #f9f9f7;
					text-align: center;
					font-size: 28rpx;
					box-sizing: border-box;
					position: relative;
					left: 5rpx;
					
					
					&:not(:first-child) {
						margin-top: 1px;
					}

					&.active,
					&:active {
						// color: #42b983;
						background-color: #fff;
						// margin-left: -20rpx;

						&::after {
							content: "";
							display: inline-block;
							clear: both;
							width: 8rpx;
							height: 36rpx;
							background-color: #d9001b;
							border-radius: 10rpx;
							// margin-left: -20rpx;
							margin-right: 10rpx;
							position: absolute;
							top: 46rpx;
							// left: 15rpx;
							left: 0;
						}
					}
					
				}
				.fz {
					font-size: 22rpx !important;
				}
				
			}

			.main {
				height: 100%;
				width: 520rpx;
				margin: 20rpx auto;

				.tips {
					line-height: 64rpx;
					font-size: 24rpx;
					font-weight: bold;
					color: #666;
					height: 64rpx;
					position: fixed;
					top: 44px;
					right: 0;
					width: 530rpx;
					z-index: 10;
					background-color: #fff;
					padding-left: 10rpx;
				}

				.title {
					line-height: 64rpx;
					position: relative;
					font-size: 24rpx;
					font-weight: bold;
					color: #666;
					height: 64rpx;
				}

				.item {
					width: 100%;
					height: 130rpx;
					// background-color: skyblue;

					>text {
						font-size: 20rpx;
						font-weight: bold;
					}

					.communicationItem {
						width: 670rpx;
						height: 130rpx;
						margin: auto;
						box-sizing: border-box;
						padding-top: 22rpx; // line-height: 130rpx;
						// padding-left: 117rpx;
						border-bottom: 1rpx solid #eee;

						.headPortrait {
							padding-left: 33rpx;

							image {
								width: 88rpx;
								height: 90rpx;
								border-radius: 50%;
							}
						}

						.name {
							// margin-left: 146rpx;
							margin-top: -93rpx;

							// text-align: left;
							// margin-left: 20rpx;
							.name {
								font-size: 28rpx;
								font-weight: bold;
								padding-left: 146rpx;
							}

							.company {
								color: #656b6c;
								font-size: 22rpx;
								padding-left: 146rpx;
								width: 500rpx;
								white-space: nowrap;
								overflow: hidden;
								text-overflow: ellipsis;
							}
						}
					}
				}

				.goods {
					display: flex;
					flex-direction: row;
					flex-wrap: nowrap;
					justify-content: flex-start;
					align-items: center;
					align-content: center;
					margin-bottom: 10rpx;

					&>image {
						width: 120rpx;
						height: 120rpx;
						margin-right: 16rpx;
					}

					.describe {
						font-size: 24rpx;
						color: #999;
					}

					.money {
						font-size: 24rpx;
						color: #efba21;
					}
				}
			}
		}
	}
</style>
